package cn.zjh.user.controller;

import cn.zjh.user.dto.BinderDto;
import cn.zjh.user.dto.LoginDto;
import cn.zjh.basic.exception.BusinessException;
import cn.zjh.basic.util.AjaxResult;
import cn.zjh.user.service.impl.LogininfoServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RequestMapping("/login")
@RestController
public class LoginController {
    @Autowired
    private LogininfoServiceImpl logininfoService;

    /**
     * 账号登录接口
     *
     * @param loginDto
     * @return
     */
    @PostMapping("/account")
    public AjaxResult accountLogin(@RequestBody LoginDto loginDto) {
        try {
            Map<String, Object> map = logininfoService.accountLogin(loginDto);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "系统异常，请稍后重试!!!");
        }
    }

    /**
     * 微信扫码登录接口
     *
     * @param
     * @return
     */
    @GetMapping("/wechat/{code}")
    public AjaxResult wechatLogin(@PathVariable("code") String code) {
        try {
            return logininfoService.wechatLogin(code);
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "系统异常，请稍后重试!!!");
        }
    }

    /**
     * 微信扫码绑定接口
     * @param
     * @return
     */
    @PostMapping("/wechat/binder")
    public AjaxResult binderLogin(@RequestBody BinderDto binderDto){
        try {
            return logininfoService.binderLogin(binderDto);
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e){
            e.printStackTrace();
            return new AjaxResult(false,"系统异常，请稍后重试!!!");
        }
    }

    /**
     * 前台账号退出
     *
     * @param
     * @return
     */
    @GetMapping("/logout/{token}")
    public AjaxResult loginOut(@PathVariable("token") String token) {
        try {
                logininfoService.logOut(token);
                return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "系统异常，请稍后重试!!!");
        }
    }

}